import { defineStore } from 'pinia'
import { getExpiredStorage } from './expireStorage.js'


const expiresTime = 60 * 60 * 24 // 默认过期时间 24h 
let expiredStorage = getExpiredStorage(localStorage, expiresTime)
const userInfoStore = defineStore('user', {
  state: () => ({
    userInfo: null,
    token: null,
  }),
  actions: {
    LoginIn(data) {
      this.token = data.token;
      this.userInfo = data.userInfo;
      expiredStorage = getExpiredStorage(localStorage, data.expire);
    },
    LoginOut() {
      localStorage.removeItem('user');
      location.href = '/';
    }
  },
  //持久化
  persist: {
    key: 'user', //存储名称
    storage: expiredStorage, // 存储方式
    paths: null, //指定 state 中哪些数据需要被持久化。[] 表示不持久化任何状态，undefined 或 null 表示持久化整个 state
  }
});

export default userInfoStore;